package com.jswlw.controller.devapi;

import com.alibaba.fastjson.JSON;
import com.jswlw.common.BaseApiResponse;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.controller.BaseController;
import com.jswlw.controller.devapi.adapter.DevApiAdapter;
import com.jswlw.controller.devapi.req.*;
import com.jswlw.controller.devapi.resp.DevSetResponse;
import com.jswlw.controller.devapi.resp.IdResponse;
import com.jswlw.entity.biz.DevInfo;
import com.jswlw.entity.biz.DevSet;
import com.jswlw.schedule.DevSetSchedule;
import com.jswlw.service.biz.DevInfoService;
import com.jswlw.service.biz.DevSetService;
import com.jswlw.service.helper.message.ReadOrModifyServiceTimeMessage;
import com.jswlw.service.helper.message.ReadOrModifyWashFixedTimeMessage;
import com.jswlw.service.helper.message.ReadOrModifyWashTimeMessage;
import com.jswlw.service.helper.message.RwOneWaterOfTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Slf4j
@Api(tags = "设备调用接扣")
@RestController
@RequestMapping("/devApi")
public class DevApiController extends BaseController {

    @Autowired
    DevApiAdapter adapter;
    @Autowired
    DevSetService devSetService;
    @Autowired
    DevInfoService devInfoService;

    @ApiOperation(value = "心跳检测", notes = "心跳检测")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("heartbeat")
    public BaseApiResponse heartbeat(@RequestBody HeartbeatMessage message) {
        adapter.heartbeat(message);
        return BaseApiResponse.of().withResultSuccess();
    }

    @ApiOperation(value = "申请设备ID", notes = "申请设备ID")
    @ApiResponse(code = 200, message = "OK", response = IdResponse.class)
    @PostMapping("gainDeviceId")
    public IdResponse createId(@RequestBody GainDeviceIdMessage message) {
        return adapter.createId(message);
    }

    @ApiOperation(value = "用水同步", notes = "用水同步")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("asyncWater")
    public BaseApiResponse asyncWater(@RequestBody SyncWaterMessage message) {
        adapter.asyncWater(message);
        return BaseApiResponse.of().withResultSuccess();
    }

    @ApiOperation(value = "工作状态同步", notes = "工作状态同步")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("asyncWorkStatus")
    public BaseApiResponse asyncWorkStatus(@RequestBody SyncWorkStatusMessage message) {
        adapter.asyncWorkStatus(message);
        return BaseApiResponse.of().withResultSuccess();
    }

    @ApiOperation(value = "时间模式下，上传泵工使用时长", notes = "时间模式下，上传泵工使用时长")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("asyncDay")
    public BaseApiResponse asyncDay(@RequestBody SyncDayMessage message) {
        adapter.asyncDay(message);
        return BaseApiResponse.of().withResultSuccess();
    }

    @ApiOperation(value = "更新iccid")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("updateIccid")
    public BaseApiResponse updateIccid(@RequestBody ReadIccidFromDeviceMessage message) {
        adapter.updateIccid(message);
        return BaseApiResponse.of().withResultSuccess();
    }

    @ApiOperation(value = "两次水满制水超过1分钟,再次水满冲洗时间，单位秒")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("rw_wash_time")
    public DevSetResponse rw_wash_time(@RequestBody ReadOrModifyWashTimeMessage request) {
        log.info("冲洗参数：" + JSON.toJSONString(request));
        DevSetResponse response = new DevSetResponse();
        try {
            DevSet devSet = devSetService.find4DevId(devInfoService.find4Imei(request.getImei()).getId());
            devSet.setWashTime(Integer.valueOf(request.getSeconds()));
            devSetService.update(devSet);
            response.setData(DevSetResponse.DevSetParam.builder().value(devSet.getWashTime()).build());
        } catch (Exception e) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "设备不存在");
        }
        response.success();
        return response;
    }

    @ApiOperation(value = "读/改检修时间，单位分钟")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("rw_service_time")
    public DevSetResponse rw_service_time(@RequestBody ReadOrModifyServiceTimeMessage request) {
        log.info("刷新检修时长:" + JSON.toJSONString(request));
        DevSetResponse response = new DevSetResponse();
        try {
            DevSet devSet = devSetService.find4DevId(devInfoService.find4Imei(request.getImei()).getId());
            devSet.setServiceTime(Integer.valueOf(request.getMinutes()));
            devSetService.update(devSet);
            response.setData(DevSetResponse.DevSetParam.builder().value(devSet.getServiceTime()).build());
        } catch (Exception e) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "设备不存在");
        }
        response.success();
        return response;
    }

    @ApiOperation(value = "读/改定时冲洗时间，单位分钟")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("rw_wash_fix_time")
    public DevSetResponse rw_wash_fix_time(@RequestBody ReadOrModifyWashFixedTimeMessage request) {
        log.info("定时冲洗参数：" + JSON.toJSONString(request));
        DevSetResponse response = new DevSetResponse();
        try {
            DevSet devSet = devSetService.find4DevId(devInfoService.find4Imei(request.getImei()).getId());
            devSet.setWashFixedTime(Integer.valueOf(request.getMinutes()));
            devSetService.update(devSet);
            response.setData(DevSetResponse.DevSetParam.builder().value(devSet.getWashFixedTime()).build());
        } catch (Exception e) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "读/改定时冲洗时间，单位分钟");
        }
        response.success();
        return response;
    }

    @ApiOperation(value = "时间租赁模式，时长记录应用参数 (读取和修改)")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("rw_run_time")
    public DevSetResponse rw_run_time(@RequestBody PumpWorkTimeMessage request) {
        log.info("泵工作时长：" + JSON.toJSONString(request));
        DevSetResponse response = new DevSetResponse();
        try {
            DevSet devSet = devSetService.find4DevId(devInfoService.find4Imei(request.getImei()).getId());
            devSet.setPumpWorkTime(request.getMinutes());
            devSetService.update(devSet);
            response.setData(DevSetResponse.DevSetParam.builder().value(devSet.getPumpWorkTime()).build());
        } catch (Exception e) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "设备不存在");
        }
        response.success();
        return response;
    }

    @ApiOperation(value = "制1升水需要的时间，读写，单位分钟")
    @ApiResponse(code = 200, message = "OK", response = BaseApiResponse.class)
    @PostMapping("rw_one_water_of_time")
    public DevSetResponse rw_one_water_of_time(@RequestBody RwOneWaterOfTime request) {
        log.error("读写数据:" + JSON.toJSONString(request));
        DevSetResponse response = new DevSetResponse();
        DevSet devSet = devSetService.find4DevId(devInfoService.find4Imei(request.getImei()).getId());
        devSet.setOneWaterOfTime(request.getMinutes());
        devSetService.update(devSet);
        response.success();
        return response;
    }

}
